package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.gts.bpaas.middleware.mq.api.anns.MQConsumer;
import com.aliyun.gts.bpaas.middleware.mq.api.consumer.ConsumeEventProcessor;
import com.aliyun.gts.bpaas.middleware.mq.api.model.StandardEvent;
import com.aliyun.gts.bpaas.middleware.mq.api.model.Transferable;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

/**
 * @author lilh
 * @date 2021/4/23 17:15
 */
@Slf4j
@ConditionalOnProperty(name = "mq.consumer.test.enable", havingValue = "true", matchIfMissing = false)
@MQConsumer(groupId = "${my.mq.consumer.groupId:GID_DEV_FLOW_TASK_CONSUMER}", topic = "${mq.topic:dev_topic_flow_task}")
public class AnnotatedMQListener implements ConsumeEventProcessor {

    @Override
    public boolean process(StandardEvent event) {
        Transferable data = event.getPayload().getData();
        try {
            log.info("annotation receive StandardEvent: {}", JSONObject.toJSONString(data));
            // TODO something
            return true;
        } catch (Exception ex) {
            log.error("failed to process message ORDER_DONE: {}, cause: {}",
                    JSONObject.toJSONString(data), Throwables.getStackTraceAsString(ex));
            return false;
        }
    }
}